const fs = require('fs');
const path = require('path');

module.exports = (app) => {
    const routeDirs = fs.readdirSync(path.join(__dirname, '../routes'))
        .filter(dir => !dir.startsWith('.')); // 忽略隐藏目录

    routeDirs.forEach(dir => {
        const routePath = path.join(__dirname, '../routes', dir);
        
        // 确保是目录
        if (!fs.statSync(routePath).isDirectory()) return;

        fs.readdirSync(routePath)
            .filter(file => file.endsWith('.js') && !file.includes('test'))
            .forEach(file => {
                try {
                    const router = require(path.join(routePath, file));
                    if (router && router.routes) {
                        app.use(router.routes(), router.allowedMethods());
                        // console.log(`已注册路由: ${dir}/${file}`);
                    }
                } catch (err) {
                    console.error(`路由注册失败: ${file}`, err);
                }
            });
    });
};
